package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TAppInfo;
import hl.model.TPcOperator;
import hl.model.TStuff;
import hl.model.TVenueInfo;
import hl.pageModel.AppInfo;
import hl.pageModel.DataGrid;
import hl.service.AppInfoService;

@Service("appInfoService")
public class AppInfoServiceImpl implements AppInfoService {

	private BaseDao<TAppInfo> appInfoDao;

	public BaseDao<TAppInfo> getAppInfoDao() {
		return appInfoDao;
	}

	private BaseDao<TPcOperator> pcOperatorDao;

	public BaseDao<TPcOperator> gePcOperatorDao() {
		return pcOperatorDao;
	}
	@Autowired
	public void setPcOperatorDao(BaseDao<TPcOperator> pcOperatorDao) {
		this.pcOperatorDao = pcOperatorDao;
	}
	@Autowired
	public void setAppInfoDao(BaseDao<TAppInfo> appInfoDao) {
		this.appInfoDao = appInfoDao;
	}

	private BaseDao<TStuff> stuffDao;

	public BaseDao<TStuff> getStuffDao() {
		return stuffDao;
	}
	@Autowired
	public void setStuffDao(BaseDao<TStuff> stuffDao) {
		this.stuffDao = stuffDao;
	}
	
	
	@Override
	public DataGrid getDatagrid(AppInfo appInfo) {
		DataGrid dg = new DataGrid();
		String hql = "from TAppInfo t ";

		Map<String, Object> params = new HashMap<String, Object>();

		//hql = search(appInfo, hql, params);

		if (appInfo.getOrder() != null) {
			hql += " order by " + appInfo.getSort() + " " + appInfo.getOrder();
		}

		List<TAppInfo> l = appInfoDao.find(hql, params, appInfo.getPage(), appInfo.getRows());

		int totalHql = appInfoDao.count(hql, params);

		List<AppInfo> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TAppInfo t : l) {
				AppInfo u = new AppInfo();
				BeanUtils.copyProperties(t, u);
				u.setPkStuff(t.getTPcOperator().getTStuff().getPkStuff());
				u.setStuffName(t.getTPcOperator().getTStuff().getName());
				u.setInfoStatus(t.getInfoStatus().compareTo("0")==0?"最新":"历史");
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(AppInfo appInfo, String hql, Map<String, Object> params) {
		/*if (appInfo.getName() != null && !appInfo.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + appInfo.getName().trim() + "%");
		}

		if (appInfo.getGender() != null && !appInfo.getGender().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.gender like :value2 ";
			params.put("value2", "%" + appInfo.getGender().trim() + "%");
		}
		if (appInfo.getPkappInfo() != null && !appInfo.getPkappInfo().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.pkappInfo like :value3 ";
			params.put("value3", "%" + appInfo.getPkappInfo().toString() + "%");
		}*/
		return hql;
	}

	@Override
	public void save(AppInfo appInfo) {
		TAppInfo t = new TAppInfo();
		BeanUtils.copyProperties(appInfo, t);
		t.setCdatetime(new Date());
		t.setTPcOperator(pcOperatorDao.get("from TPcOperator t where t.TStuff.pkStuff=?", new Object[] {appInfo.getPkStuff()}));
		appInfoDao.save(t);
	}

	@Override
	public void edit(AppInfo appInfo) {
		TAppInfo t = new TAppInfo();
		BeanUtils.copyProperties(appInfo, t);
		t.setTPcOperator(pcOperatorDao.get("from TPcOperator t where t.TStuff.pkStuff=?", new Object[] {appInfo.getPkStuff()}));
		t.setMdatetime(new Date());
		appInfoDao.saveOrUpdate(t);
	}

	@Override
	public void remove(AppInfo appInfo) {
		TAppInfo t = appInfoDao.get(TAppInfo.class, appInfo.getPkAppInfo());
		appInfoDao.delete(t);
	}

	@Override
	public Object getComboBox(AppInfo appInfo) {
		List<TAppInfo> l = appInfoDao.find("from TAppInfo");
		List<AppInfo> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TAppInfo t : l) {
				AppInfo u = new AppInfo();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		return pl;
	}

}
